% im can either be an image or an existing open figure

function roi = selectDivingROIs(im)
%normmov = (mov-min(mov(:)))/(max(mov(:))-min(mov(:)));
%im = mean(normmov(:,:,20:30));
if isa(im,'double')
    figure;
    imagesc(im);
    displayax = gca;
else
    figure(im);
    displayax = gca;
end
% will have 4 columns; first column is first index of M (row), second
% column is last index of M, third column is first index of N (column) and
% fourth index is last index of N. Each row is a separate ROI
roi = [];

disp('keep selection and continue [1] \ndiscard selection and re-select [2] \nkeep selection and end [3] \ndiscard selection and end [4]\n');
while true
    
    temp = drawrectangle(displayax);
    
    s = input('keep selection and continue [1] \ndiscard selection and re-select [2] \nkeep selection and end [3] \ndiscard selection and end [4]\n');
    switch s
        case 1
            p = round(temp.Position);
            M = [p(2),(p(2)+p(4)-1)];
            N = [p(1),(p(1)+p(3)-1)];
            roi = [roi;M,N];
        case 2
            continue;
        case 3
            p = round(temp.Position);
            M = [p(2),(p(2)+p(4)-1)];
            N = [p(1),(p(1)+p(3)-1)];
            roi = [roi;M,N];
            break;
        case 4
            break;
        otherwise
            disp('invalid selection')
            continue;
    end
end

